package main

import (
	"context"
	"fmt"
	"github.com/apache/thrift/lib/go/thrift"
	"rpc/thrift/t1/user"
	"strconv"
)

type UserService struct{}

func (u *UserService) GetUser(ctx context.Context, id int32) (r *user.UserInfo, err error) {
	return &user.UserInfo{ID: int64(id),}, nil
}

func (u *UserService) GetName(ctx context.Context, id int32) (r string, err error) {
	return strconv.Itoa(int(id)) + "是张三", nil
}

func main() {
	// 服务处理（实际业务实现如上面）
	handler := &UserService{}
	processor := user.NewUserProcessor(handler) // NewUserProcessor User就是我们的User

	// 网络传输方式（客户端与服务端需一致）
	transport, err := thrift.NewTServerSocket("localhost:9000")
	if err != nil {
		fmt.Printf("thrift NewTServerSocket error, %s\n", err)
		return
	}

	// 服务模型（采用TBinaryProtocol传输格式，客户端与服务端需一致）
	server := thrift.NewTSimpleServer2(processor, transport)

	fmt.Printf("server run ...\n")
	server.Serve()
}
